package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.util.ResultUtil;
import cn.uniqueinfo.dp.vo.PermissionVO;
import cn.uniqueinfo.dp.vo.Result;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.service.IRolePermissionService;
import cn.uniqueinfo.dp.vo.RolePermissionVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-02-13 11:14:42
 * @description:角色权限对应表
 */
@Slf4j
@Controller
@RequestMapping(value = "/rolepermission")
public class RolePermissionController extends BaseController<RolePermissionVO> {
    private IRolePermissionService service;

    public RolePermissionController(IRolePermissionService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "rolepermission";
        this.tipName = "角色权限对应表";
    }

    @PostMapping(value = "/rp")
    @ResponseBody
    public Result saveRolePermissions(Long rid, @RequestParam(value = "fn") String formName, String rps) {
        if (rps == null || rps.length() == 0) {
            return ResultUtil.fail();
        }
        try {
            //this.service.deleteByRoleId(rid);

            this.service.deleteByRoleId(rid,formName);
            List<PermissionVO> permissionList = JSONObject.parseArray(rps, PermissionVO.class);
            for (PermissionVO vo : permissionList) {
                RolePermissionVO rpvo = new RolePermissionVO();
                rpvo.setPid(vo.getId());
                rpvo.setRid(rid);
                this.service.insert(rpvo);
            }
            return ResultUtil.success();

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.fail();
        }
    }
}
